package com.yan.qing.juc.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @description: ABA问题
 * @author: lf
 * @create: 2022年03月13日 23:35:00
 */
public class AtomicStampedReferenceABA {
    private static AtomicInteger index=new AtomicInteger (10);
   // static AtomicStampedReference<Integer> stampedReference= new AtomicStampedReference(10,1);

    //ABA问题
    public static void main (String[] args) {
        new Thread (()->{
           index.compareAndSet (10,11);
           index.compareAndSet (11,10);
            System.out.println (Thread.currentThread().getName() + "-----10-11-10");

        },"张三").start ();
        new Thread (()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                boolean isSuccess = index.compareAndSet (10, 12);
                System.out.println (Thread.currentThread().getName() + "预期是10吗?"+isSuccess+"设置新的值:"+index.get());
            } catch ( InterruptedException e ) {
                e.printStackTrace ();
            }
        },"李四").start ();
    }
}